package com.systop.dxjj.modules.student.service.impl;

import com.systop.dxjj.common.pojo.*;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.student.mapper.StudentMapper;
import com.systop.dxjj.modules.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    @Qualifier("stuauthenticationManagerBean")
    private AuthenticationManager stuauthenticationManager;

    @Override
    public R loginStudent(Student student) {
        Student s = studentMapper.loginStudent(student);
        if (s == null) {
            return R.ok().put("start","0");
        }else {
            return R.ok().put("start","1");
        }
    }

    @Override
    public R selectAllStudents() {
        List<Student> list = studentMapper.selectAllStudents();
        return R.ok().put("students",list);
    }

    @Override
    public R selectStudentById(Integer id) {
        Student student = studentMapper.selectStudentById(id);
        return R.ok().put("students",student);
    }

    @Override
    public R selectStudentByName(String name) {
        Student student = studentMapper.selectStudentByName(name);
        return R.ok().put("students",student);
    }


    @Override
    public R selectStudentByVagueName(String name) {
        List<Student> student = studentMapper.selectStudentByVagueName(name);
        return  R.ok().put("students",student);
    }

    @Override
    public R insertStudent(Student student) {
        int num = studentMapper.insertStudent(student);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public R deleteStudent(Integer id) {
        int student = studentMapper.deleteStudent(id);
        return R.ok().put("student",student);
    }

    @Override
    public R updateStudent(Student student) {
        int num = studentMapper.updateStudent(student);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public Student selectByPhone(String studentphone) {
        return studentMapper.selectByPhone(studentphone);
    }

    @Override
    public ResponseResult studentLogin(Student student) {
        //进行用户认证
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(student.getStudentphone(),student.getStudentpassword());
        stuauthenticationManager.authenticate(usernamePasswordAuthenticationToken);


        Authentication authenticate = stuauthenticationManager.authenticate(usernamePasswordAuthenticationToken);
        //如果认证没通过给出提示
        if (Objects.isNull(authenticate)){
            throw new RuntimeException("登录失败");
        }
        //如果认知通过，使用stuid生成jwt，jwt存入ResponseResult返回
        StudentUser studentUser = (StudentUser)authenticate.getPrincipal();
        String stuid = studentUser.getStudent().getStudentid().toString();
        String jwt = JwtUtil.createJWT(stuid);
        //把完整的用户信息存入redis，userid作为key
        Map<String, String> map = new HashMap<>();
        map.put("token",jwt);
        //把完整的用户信息存入redis，stuid作为key
        redisCache.setCacheObject("stulogin:"+stuid,studentUser);
        return new ResponseResult(200,"登陆成功！",map);
    }

    @Override
    public ResponseResult studentlogout() {
        //获取SecurityContextHolder中的用户id
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        StudentUser studentUser = (StudentUser) authentication.getPrincipal();
        Integer stuid = studentUser.getStudent().getStudentid();
        //删除redis中的值
        redisCache.deleteObject("stulogin:"+stuid);
        return new ResponseResult(200,"注销成功");
    }

}
